<?php

namespace App\Modules\LuckDraw\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class LuckDrawModel  extends Model
{
    protected $table = "luck_draw";

    public static function get($luckId)
    {
        return self::query()
            ->where('id', $luckId)
            ->where('status', 1)
            ->where('deleted', 0)
            ->first();
    }

    public static function getActivityInfo($field = ['*'])
    {
        $data = self::query()
            ->where('status', 1)
            ->where('deleted', 0)
            ->get($field);
        return self::objToArray($data);
    }

    //返回数组
    public static function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }

    public function setStrategyPool($id, $strategyPoolData)
    {
        return DB::table($this->table)->where('id', $id)->update(['strategy_pool' => $strategyPoolData, 'updated_at' => date('Y-m-d H:i:s')]);
    }

    public static function updateStrategyPool($id, $strategyPoolData){
        return self::query()
            ->where('id', $id)
            ->update([
                'strategy_pool' => $strategyPoolData
            ]);
    }

    public function getLuckDrawById($id)
    {
        $update['updated_at'] = date('Y-m-d H:i:s');
        $info = DB::table($this->table)->find($id);
        return self::objToArray($info);
    }

    public function editLuckDraw($id, $update)
    {
        $update['updated_at'] = date('Y-m-d H:i:s');
        return DB::table($this->table)
            ->where('id', $id)
            ->update($update);
    }
}
